package com.mandao.concurrency.interviewset.backdemo;

import java.util.Random;
import java.util.concurrent.*;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * Description
 *      业务窗口
 * @author woniu
 * @date 2019/6/29 11:34
 */
public class ServiceWindow {

    /**
     * 客户类型
     */
    private CustomerType type = CustomerType.COMMON;
    /**
     * 窗口号码
     */
    private int windowId = 1;

    public CustomerType getType(){
        return type;
    }
    public void setType(CustomerType type) {
        this.type = type;
    }
    public void setWindowId(int windowId) {
        this.windowId = windowId;
    }

//    ThreadPoolExecutor executor =
//            new ThreadPoolExecutor(5, 10, 1, TimeUnit.MINUTES,
//                    new ArrayBlockingQueue<Runnable>(10),
//                    new ThreadPoolExecutor.CallerRunsPolicy());


    /**
     *  线程池执行任务
     */
    public void start(){
        //匿名内部类
        Executors.newSingleThreadExecutor().execute(()->{
                while(true){
                    switch(type){
                        case COMMON:
                            commonService();
                            break;
                        case EXPRESS:
                            expressService();
                            break;
                        case VIP:
                            vipService();
                            break;
                        default:
                            vipService();
                    }
            }
        });
    }

    /**
     * 普通客户
     */
    private void commonService() {
        String windowName = windowId + "号" + type +"窗口";
        System.out.println(windowName+"正在获取任务...");
        Integer number = NumberMachine.getInstance().getCommonManager().fetchServiceNumber();
        if(number !=null){
            int maxRandom = Constants.Max_Service_Time - Constants.Min_Service_Time;
            long serverTime = new Random().nextInt(maxRandom)+1+Constants.Min_Service_Time;
            try {
                //服务时间
                Thread.sleep(serverTime);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(windowName + "完成为第" +number+"个普通客户服务；耗时: "+serverTime+" Millis");
        }
        else{
            System.out.println(windowName+"没有任务...休息1秒钟...");
            try {
                //等待时间
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * VIP客户
     */
    private void vipService() {
        String windowName = windowId + "号" + type +"窗口";
        System.out.println(windowName+"正在获取任务...");
        Integer number = NumberMachine.getInstance().getVipManager().fetchServiceNumber();
        if(number !=null){
            int maxRandom = Constants.Max_Service_Time - Constants.Min_Service_Time;
            long serverTime = new Random().nextInt(maxRandom)+1+Constants.Min_Service_Time;
            try {
                //服务时间
                Thread.sleep(serverTime);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(windowName + "完成为第" +number+"个"+type+"客户服务；耗时: "+serverTime+" Millis");
        }
        else{
            System.out.println(windowName+"没有VIP任务...为普通客户服务...");
            commonService();	//为普通客户服务

        }
    }


    /**
     *  快速通道
     */
    private void expressService() {
        String windowName = windowId + "号" + type +"窗口";
        System.out.println(windowName+"正在获取任务...");
        Integer number = NumberMachine.getInstance().getExpressManager().fetchServiceNumber();
        if(number !=null){
            int serviceTime = Constants.Min_Service_Time;
            try {
                //快速客户服务时间，最小值
                Thread.sleep(serviceTime);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(windowName + "完成为第" +number+"个"+type+"客户服务；耗时: "+serviceTime+" Millis");
        }
        else{
            System.out.println(windowName+"没有快速任务...为普通客户服务...");
            commonService();	//为普通客户服务

        }
    }

}
